package com.zhm.demo.mqtt;

import com.alibaba.fastjson.JSONObject;
import com.zhm.demo.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;

@Slf4j
public class MyMqttClient {

    private static String broker = "tcp://10.101.0.64:1883";
    private static String imei = "330022320240308";
    private static String sn = "24330022320240308";
    private static String username = "admin";
    private static String serverkey = "k0ekpKNA55m";
    private static String password = "382346819(){}|Pl";

    private static MqttClient sampleClient;

    public static void main(String[] args) throws Exception {
        connect();
        while (true) ;
    }

    public static void connect() throws MqttException, InterruptedException {
        //接入物联网平台的域名。


        String clientId = imei;
        // Paho MQTT客户端。
        sampleClient = new MqttClient(broker, clientId);

        // Paho MQTT连接参数。
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(true);
        connOpts.setKeepAliveInterval(180);
        connOpts.setUserName(username);
        connOpts.setPassword(password.toCharArray());
        connOpts.setAutomaticReconnect(true);
        sampleClient.connect(connOpts);
        System.out.println("Broker: " + broker + " Connected");



        String upgradeTopic = "/ota/device/upgrade/"+serverkey+"/"+imei;
        log.info("subscribe upgradeTopic {}",upgradeTopic);
        sampleClient.subscribe(upgradeTopic, new MqttPostPropertyMessageListener());


        String check2GTopic = "/check/2g/status/"+serverkey+"/"+imei+"/down";
        log.info("subscribe check2GTopic {}",check2GTopic);
        sampleClient.subscribe(check2GTopic, new MqttPostPropertyMessageListener());

        String downlinkTopic = "/sys/device/devicestatus/"+serverkey+"/"+imei+"/downlink";
        log.info("subscribe downlinkTopic {}",downlinkTopic);
        sampleClient.subscribe(downlinkTopic, new MqttPostPropertyMessageListener());

        String deviceSettingsTopic = "/update/device/settings/"+imei+"/set";
        log.info("subscribe deviceSettingsTopic {}",deviceSettingsTopic);
        sampleClient.subscribe(deviceSettingsTopic, new MqttPostPropertyMessageListener());

        String ntpResTopic = "/ext/ntp/"+serverkey+"/"+imei+"/response";
        log.info("subscribe ntpResTopic {}",ntpResTopic);
        sampleClient.subscribe(ntpResTopic, new MqttPostPropertyMessageListener());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("deviceSendTime", ""+ DateUtils.getTsMsByTwoThousandYearsAgo(System.currentTimeMillis()));
        while (true){
            Thread.sleep(5000);
            send("/ext/ntp/"+serverkey+"/"+imei+"/request",jsonObject.toJSONString().getBytes());
        }
    }

    public static void send(String topic, byte[] content) throws MqttException {
        // Paho MQTT发布消息。
//        String topic = "/sys/" + productKey + "/" + deviceName + "/thing/event/property/post";
//        String content = "{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":1}}";
        MqttMessage message = new MqttMessage(content);
        message.setQos(2);
        sampleClient.publish(topic, message);
    }

    public static class MqttPostPropertyMessageListener implements IMqttMessageListener {
        @Override
        public void messageArrived(String var1, MqttMessage var2) throws Exception {
            log.info("reply topic:{} body:{}" , var1,var2);
        }
    }

}
